package com.pc.admin.service.ipml;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pc.admin.dto.CelebritySaveDto;
import com.pc.admin.entity.CelebritySavePO;
import com.pc.admin.exception.VerifyException;
import com.pc.admin.mapper.CelebritySaveMapper;
import com.pc.admin.service.CelebritySaveService;
import com.pc.admin.util.CommonResult;
import com.pc.admin.vo.CelebrityTkIdVO;
import com.pc.admin.vo.DataType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class CelebritySaveServiceImpl extends ServiceImpl<CelebritySaveMapper, CelebritySavePO> implements CelebritySaveService {


    @Autowired
    private CelebritySaveMapper celebritySaveMapper;

    @Autowired
    private CelebritySaveService celebritySaveService;

    @Override
    public void saveOrUpdate(List<CelebritySaveDto> list) {
        // 判断传入的列表是否为空，如果为空则直接返回提示
        if (CollectionUtil.isEmpty(list)) {
            throw new VerifyException("参数异常");
        }
        // 从传入的列表中提取出所有的TkId，得到一个id列表
        List<String> idList = list.stream().map(CelebritySaveDto::getTkId).collect(Collectors.toList());

        // 查询数据库，获取CelebritySavePO对象列表
        List<CelebritySavePO> poList = celebritySaveMapper.selectList(new LambdaQueryWrapper<CelebritySavePO>()
                .in(CollectionUtil.isNotEmpty(idList), CelebritySavePO::getTkId, idList));

        // 将查询到的CelebritySavePO对象列表转换为一个Map，键为TkId，值为Id
        Map<String, Integer> map = Optional.ofNullable(poList).orElse(new ArrayList<>()).stream()
                .collect(Collectors.toMap(CelebritySavePO::getTkId, CelebritySavePO::getId, (o1, o2) -> o1));

        // 遍历参数列表，如果某个元素的TkId在map中存在，则将其对应的Id设置为map中的值
        list.forEach(item -> {
            if (map.containsKey(item.getTkId())) {
                item.setId(map.get(item.getTkId()));
            }
        });
        // 创建一个新的CelebritySavePO对象的列表，用于存储转换后的数据
        List<CelebritySavePO> celebritySavePOS = new ArrayList<>();

        // 遍历参数列表，将Tendency属性转换为JSON字符串
        list.forEach(item -> {
            CelebritySavePO po = BeanUtil.copyProperties(item, CelebritySavePO.class);
            po.setTendency(JSON.toJSONString(item.getTendency()));
            if (po.getDataState() == 0 || po.getDataState() == null) {
                po.setDataState(0);
                celebritySavePOS.add(po);
            } else if (po.getDataState() == 1 || po.getDataState() == null) {
                po.setDataState(1);
                celebritySavePOS.add(po);
            }
        });
        // 批量保存或更新数据
        celebritySaveService.saveOrUpdateBatch(celebritySavePOS);
        log.info("操作数据成功！");
    }


    @Override
    public CommonResult<List<CelebrityTkIdVO>> getTkIdList() {
        List<CelebritySavePO> poList = celebritySaveMapper.selectTkIdList();
        List<CelebrityTkIdVO> dataState0List = new ArrayList<>();
        CelebrityTkIdVO celebrityTkIdVO = new CelebrityTkIdVO();
        DataType dataType = new DataType();
        for (CelebritySavePO po : poList) {
            if (po.getTkId() == null || StrUtil.isBlank(po.getTkId())
                    || po.getUsername() == null
                    || StrUtil.isBlank(po.getUsername())
                    || po.getDataState() == null) {
                throw new VerifyException("数据为空");
            }
            if (po.getDataState() == 0) {
                celebrityTkIdVO.setUpdateType(po.getDataState());
                dataType.setTkId(po.getTkId());
                dataType.setUsername(po.getUsername());
                celebrityTkIdVO.setDataTypes(Arrays.asList(dataType));
                dataState0List.add(celebrityTkIdVO);
            } else if (po.getDataState() == 1) {
                celebrityTkIdVO.setUpdateType(po.getDataState());
                dataType.setTkId(po.getTkId());
                dataType.setUsername(po.getUsername());
                celebrityTkIdVO.setDataTypes(Arrays.asList(dataType));
                dataState0List.add(celebrityTkIdVO);
            }
        }
        return CommonResult.ok(dataState0List);
    }


}













